<?php
//base
function connect_to_db(){
    $servername = "localhost";
    $username = "test";
    $password = "123456";
    $dbname = "bookmarksdb";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
	die("Connection failed: " . $conn->connect_error);
    }

    return $conn;
} 

function query_table($sql){
    $conn = connect_to_db();
    $result = $conn -> query($sql);
    return $result;
    $conn->close();
}

function build_resp_dir_info($DirID,$DirName,$ParentDirID)
{
    $arr['cmd'] = "ServerResDirInfo"; 
    $arr['DirID'] = $DirID; 
    $arr['DirName'] = $DirName; 
    $arr['ParentDirID'] = $ParentDirID; 
    return $arr;
}

function build_resp_bookmark_info($BookMarkID,$BookMarkName,$BookMarkLink,$BookMarkDirID)
{
    $arr['cmd'] = "ServerResBookMarkInfo";
    $arr['BookMarkID'] = $BookMarkID;
    $arr['BookMarkName'] = $BookMarkName;
    $arr['BookMarkLink'] = $BookMarkLink;
    $arr['BookMarkDirID']= $BookMarkDirID;
    return $arr;
}

function get_bookmark_info()
{
    $sql = "select BookMarkID,BookMarkName,BookMarkLink,BookMarkDirID from BookMark";
    $result = query_table($sql);
    if ( $result->num_rows > 0)
    {
	$recordSet = array();
	while ( $row = $result->fetch_assoc())
	{
	    $recordSet[] = build_bookmark_info($row["BookMarkID"],$row["BookMarkName"],$row["BookMarkLink"],$row["BookMarkDirID"]);
	}

	echo json_encode($recordSet);
    }
    else
    {
	//echo "0 result.";
    }

}

function get_dir_info()
{
    $sql = "select DirID,DirName,ParentDirID from BookMarkDir";
    $result = query_table($sql);
    if ( $result->num_rows > 0)
    {
	$recordSet = array();
	while ( $row = $result->fetch_assoc())
	{
	    $recordSet[] = build_resp_dir_info($row["DirID"],$row["DirName"],$row["ParentDirID"]);
	}

	echo json_encode($recordSet);
    }
    else
    {
	//echo "0 result.";
    }

}
function insert_bookmark($BookMarkID,$BookMarkName,$BookMarkLink,$BookMarkDirID){
    $conn = connect_to_db();
    $sql = "INSERT INTO BookMark(BookMarkID,BookMarkName,BookMarkLink,BookMarkDirID)
    VALUES ('$BookMarkID','$BookMarkName', '$BookMarkLink','$BookMarkDirID')";

    if ($conn->query($sql) === TRUE) {
	$recordSet = array();
	$recordSet[] = build_resp_bookmark_info($BookMarkID,$BookMarkName,$BookMarkLink,$BookMarkDirID);
	echo json_encode($recordSet);

    } else {
    }
    $conn->close();
}

function insert_new_dir($newId,$newDirName,$newParentDirID){
    $conn = connect_to_db();
    $sql = "INSERT INTO BookMarkDir(DirID,DirName,ParentDirID) VALUES ('$newId','$newDirName', '$newParentDirID')";

    if ($conn->query($sql) === TRUE) {
	$recordSet = array();
	$recordSet[] = build_resp_dir_info($newId,$newDirName,$newParentDirID);
	echo json_encode($recordSet);

    } else {
    }
    $conn->close();
}

function parseCmd($JsonRecord)
{
    $cmd = $JsonRecord->{"cmd"};
    switch($cmd)
    {
	case "ClientQueryDirInfo":
	    get_dir_info();
	    break;

	case "ClientApplyInsertDirInfo":
	    insert_new_dir($JsonRecord->{"DirID"},$JsonRecord->{"DirName"},$JsonRecord->{"ParentDirID"});
	    break;
	case "ClientApplyInsertBookMarkInfo":
	    insert_new_bookmark($JsonRecord->{"BookMarkID"},$JsonRecord->{"DirName"},$JsonRecord->{"ParentDirID"});
	    break;
    }
}

//insert_new_dir("0",'FrontEnd',"0");
//get_dir_info();
$data_back = json_decode(file_get_contents('php://input'));
parseCmd($data_back);

?>
